//
//  FindPasswordViewController.m
//  DaiShengInvest
//
//  Created by davinci on 17/6/8.
//  Copyright © 2017年 davinci. All rights reserved.
//

#import "FindPasswordViewController.h"
#import "ResetPasswordViewController.h"

@interface FindPasswordViewController ()

@property (nonatomic, weak)UIImageView * backViewImage;

@property (nonatomic, weak)UIView * backViewOne;

@property (nonatomic, weak)UILabel * telLabel;

@property (nonatomic, weak)UITextField * telTextField;

@property (nonatomic, weak)UIButton * sentMessageButton;

@end

@implementation FindPasswordViewController


//显示导航栏
-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBarHidden = NO;
    
}


- (void)viewDidDisappear:(BOOL)animated{
    
    self.telTextField.text = @"";
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self setupNavBar];
    
    [self setupUI];

}

- (void)setupNavBar{
    
    self.title = @"找回密码";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(15 * SCREENH6, 5 * SCREENH6, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToLogView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
}


- (void)setupUI{
    
    UIImageView * backImage = [[UIImageView alloc] init];
    
    backImage.image = [UIImage imageNamed:@"背景"];
    
    self.backViewImage = backImage;
    
    [self.view addSubview:self.backViewImage];
    
    
    UIView * backViewOne = [[UIView alloc] init];
    
    backViewOne.backgroundColor = [UIColor whiteColor];
    backViewOne.alpha = 0.1;
    
    self.backViewOne = backViewOne;
    
    [self.view addSubview:self.backViewOne];
    
    UILabel * telLabel = [[UILabel alloc] init];
    
    telLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    telLabel.textColor = [UIColor whiteColor];//ColorWithRGB(0xeeeeee);
    telLabel.text = @"手机号";
    
    self.telLabel = telLabel;
    
    [self.view addSubview:self.telLabel];
    
    UITextField * telText = [[UITextField alloc] init];
    
    telText.borderStyle = UITextBorderStyleNone;
    telText.backgroundColor = [UIColor clearColor];
    
    //设置光标颜色
    telText.tintColor = ColorWithRGB(0x808486);
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0x808486);
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    telText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:attrs];
    telText.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    telText.textColor = ColorWithRGB(0x808486);
    telText.keyboardType = UIKeyboardTypeNumberPad;
    telText.keyboardAppearance = UIKeyboardAppearanceDark;
    
    self.telTextField = telText;
    
    [self.view addSubview:self.telTextField];
    
    
    UIButton * sentMessageButton = [[UIButton alloc] init];
    
    [sentMessageButton setTitle:@"发送验证码" forState:UIControlStateNormal];
    sentMessageButton.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:15 * SCREENH6];//[UIFont systemFontOfSize:15];
//    [sentMessageButton setBackgroundColor:ColorWithRGB(0xa92c44)];
    [sentMessageButton setBackgroundImage:[UIImage imageNamed:@"注册"] forState:UIControlStateNormal];
    sentMessageButton.layer.borderColor = [[UIColor blackColor] CGColor];
    //边框宽度
    sentMessageButton.layer.borderWidth =1.0f;
    //处理圆角
    sentMessageButton.layer.cornerRadius =5.0f;
    [sentMessageButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [sentMessageButton addTarget:self action:@selector(pushToResetPassword) forControlEvents:UIControlEventTouchUpInside];
    
    self.sentMessageButton = sentMessageButton;
    
    [self.view addSubview:self.sentMessageButton];
    
}


- (void)viewDidLayoutSubviews{
    
    [self.backViewImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.mas_equalTo(self.view);
    }];
    
    [self.backViewOne mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(345 * SCREENH6);
        make.height.mas_equalTo(55 * SCREENH6);
        make.top.mas_equalTo(self.view).offset(90 * SCREENH6);
    }];
    
    [self.telLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backViewOne).offset(9 * SCREENH6);
        make.centerY.mas_equalTo(self.backViewOne);
        make.width.mas_equalTo(45 * SCREENH6);
    }];
    
    [self.telTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.backViewOne);
        make.leading.mas_equalTo(self.telLabel.mas_trailing).offset(42 * SCREENH6);
        make.height.mas_equalTo(self.telLabel);
        make.width.mas_equalTo(125 * SCREENH6);
    }];

    [self.sentMessageButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backViewOne.mas_bottom).offset(44 * SCREENH6);
        make.width.leading.trailing.mas_equalTo(self.backViewOne);
        make.height.mas_equalTo(55 * SCREENH6);
    }];
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [self.telTextField resignFirstResponder];
}

- (void)pushToResetPassword{
    
    [self.telTextField resignFirstResponder];
    
    ResetPasswordViewController * resetPassword = [ResetPasswordViewController new];
    
    
    
    BOOL next = [self isMobileNumber:self.telTextField.text];
    
    if (next) {
        
        NSDictionary * parametersDic = @{@"phone":self.telTextField.text,@"cate":@"2"};
        
        [[NetworkTool sharedTools] POST:@"site/sms-very" parameters:parametersDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        
        resetPassword.telNumberStr = self.telTextField.text;
        
        [self.navigationController pushViewController:resetPassword animated:YES];
    }else{
        
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请输入正确的手机号"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
//                                                                  NSLog(@"action = %@", action);
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    
}


- (BOOL)isMobileNumber:(NSString *)mobileNum {
    
    //    电信号段:133/149/153/180/181/189/173/177
    //    联通号段:130/131/132/155/156/185/186/145/176
    //    移动号段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
    //    虚拟运营商:170/171
    
    NSString *MOBILE = @"^1(3[0-9]|4[579]|5[0-35-9]|8[0-9]|7[0136-8])\\d{8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    
    return [regextestmobile evaluateWithObject:mobileNum];
}


- (void)backToLogView{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.telTextField resignFirstResponder];
    
    return YES;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
